
package cc.eddic.examinationsystem.web;

import cc.eddic.examinationsystem.core.BaseController;
import cc.eddic.examinationsystem.domain.Exam;
import cc.eddic.examinationsystem.domain.ExamQuestion;
import cc.eddic.examinationsystem.domain.Question;
import cc.eddic.examinationsystem.repository.ExamQuestionRepository;
import cc.eddic.examinationsystem.repository.ExamRepository;
import cc.eddic.examinationsystem.repository.QuestionRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/question")
public class QuestionController  extends BaseController {
    public QuestionController(QuestionRepository questionRepository, ExamRepository examRepository) {
        this.questionRepository = questionRepository;
        this.examRepository = examRepository;
    }

    @GetMapping("/questions")
    public String getQuestions(@RequestParam("selectedExamId") long selectedExamId, Model model) {
        // 获取与所选考试相关的题目列表
        List<Question> questionList = questionRepository.findAll();

        model.addAttribute("questionList", questionList);
        model.addAttribute("selectedExamId", selectedExamId);
        // 将题目列表传递到模板中
        return "paper";
    }


    @PostMapping("/generatePaper")
    public String generatePaper(@RequestParam("selectedQuestions") List<Long> selectedQuestionIds,
                                @RequestParam("selectedExamId") long selectedExamId,Model model) {
        Optional<Exam> selectedExamOptional = examRepository.findById(selectedExamId);

        if (selectedExamOptional.isPresent()) {
            Exam exam = selectedExamOptional.get();

            for (Long questionId : selectedQuestionIds) {
                Optional<Question> questionOptional = questionRepository.findById(questionId);

                if (questionOptional.isPresent()) {
                    Question question = questionOptional.get();

                    ExamQuestion examQuestion = new ExamQuestion();
                    examQuestion.setExam(exam);
                    examQuestion.setQuestion(question);
                    if(examQuestionRepository.findAll().stream().anyMatch(examQuestion1 -> examQuestion1.getQuestion().getId().equals(questionId)&&examQuestion1.getExam().getId().equals(selectedExamId))){
                        if(log.isDebugEnabled()) log.debug("本场考试的此道题已被添加过 questionID:{}, examID:{}",questionId,selectedExamId);
                    }else{
                        examQuestionRepository.save(examQuestion);
                    }
                }
            }

            List<Question> questions=new ArrayList<>();
            for(Long id:selectedQuestionIds){
                List<ExamQuestion> examQuestions=examQuestionRepository.findAll().stream()
                        .filter(examQuestion -> examQuestion.getQuestion()
                                .getId().equals(id)&&examQuestion.getExam()
                                .getId().equals(selectedExamId)).collect(Collectors.toList());
                List<Question> questionList=examQuestions.stream()
                        .map(examQuestion -> examQuestion.getQuestion()).collect(Collectors.toList());

                System.out.println("************"+questionList.size());

                questions.addAll(questionList);
            }

            model.addAttribute("examId",selectedExamId);
            model.addAttribute("questions",questions);

            return "preview-paper";
        } else {
            return "error";
        }

    }

    @GetMapping("/select-exam")
    public String showSelectExamPage(Model model) {
        List<Exam> availableExams = examRepository.findAll(); // 获取所有可用考试场次
        model.addAttribute("availableExams", availableExams); // 将考试场次列表传递到模板中
        return "select-exam";
    }


    @PostMapping("/submit-exam")
    public String selectExam(@RequestParam("selectedExam") long selectedExamId, RedirectAttributes redirectAttributes) {
        redirectAttributes.addAttribute("selectedExamId", selectedExamId);
        return "redirect:/question/questions";
    }




//    @GetMapping("/preview-paper")
//    public String previewPaper(@RequestParam("examId") long examId,
//                               @RequestParam("questionIds") List<Long> questionIds,
//                               Model model) {
//
//        Optional<Exam> examOptional = examRepository.findById(examId);
//
//        log.debug(String.valueOf(examId));
//        if (examOptional.isPresent()) {
//            Exam exam = examOptional.get();
//            model.addAttribute("examName", exam.getName());
//        }
//
//
//        List<Question> questions = questionRepository.findAllById(questionIds);
//        model.addAttribute("examId", examId);
//        model.addAttribute("questions", questions);
//
//        return "preview-paper"; // Return the view for previewing the paper
//    }








    private final QuestionRepository questionRepository;



    //依赖注入
    @Autowired
    private ExamQuestionRepository examQuestionRepository;

    @Autowired
    private ExamRepository examRepository;


}


